<template>
  <div>
    <el-card class="box-card" shadow="hover">
      <Pagination ref="pagination" :queryPage="loadPage">
        <div slot="search">
          <el-form :inline="true" :model="queryForm" size="mini">
            <el-form-item label="库名">
              <el-input v-model="queryForm.databaseName" placeholder="数据库"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="$refs.pagination.go(1);">查询</el-button>
              <el-button type="success" @click="$router.push({name:'databaseApplyForm',query:{appName:appName}})">申请
              </el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table slot="table" :data="pageData" border size="mini" style="width: 100%">
          <el-table-column label="#ID" prop="id" width="80"/>
          <el-table-column label="库名" prop="databaseName" show-overflow-tooltip width="150"/>
          <el-table-column label="上线说明" prop="remark" show-overflow-tooltip/>
          <el-table-column label="审核状态" prop="applyStatus.name" width="80"/>

          <el-table-column label="执行状态" prop="state.name" width="80">
            <template slot-scope="scope">
              <el-tag :type="getTagType(scope.row.state.code)" size="mini">{{
                  scope.row.state.name
                }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="创建时间" prop="created" width="150"/>
          <el-table-column label="更新时间" prop="updated" width="150"/>
          <el-table-column fixed="right" label="操作">
            <template slot-scope="scope">
              <el-button size="small" type="text"
                         @click="$router.push({name:'databaseApplyForm',params:{applyId:scope.row.applyId}})">查看
              </el-button>
              <el-button v-if="scope.row.applyStatus.code=='APPROVE'" size="small" type="text"
                         @click="$router.push({name:'sqlTaskExec',query:{taskId:scope.row.id}})">
                {{ scope.row.state.code == 'WAITING' ? '执行' : '结果' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </Pagination>
    </el-card>
  </div>
</template>
<script>
import {appListApi} from "@/api/manage";
import Pagination from "@/components/Pagination";
import {enumsApi} from "@/api/common";
import {sqlTaskPageApi} from "@/api/database";

export default {
  props: {
    appName: [String],
  },
  data() {
    return {
      queryForm: {},
      pageData: [],
    };
  },
  components: {Pagination},
  watch: {
    appName() {
      this.$refs.pagination.go(1);
    }
  },
  methods: {
    getTagType(code) {
      if (code == 'SUCCESS') return "success";
      if (code == 'FAIL') return "danger";
      return "info"
    },
    async loadPage(current, size) {
      let query = Object.assign({current: current, size: size, appName: this.appName}, this.queryForm);
      let data = await sqlTaskPageApi(query);
      this.pageData = data.records;
      return data;
    },
  },
  async created() {
    this.appList = await appListApi();
    this.databaseTypes = await enumsApi("DatabaseType");
  }
};
</script>